package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class AddFriendContract {
    public AddFriendContract() { }
    public AddFriendContract(String friendID, String catalog)
    {
        this.FriendID = friendID;
        this.CatalogName = catalog;
    }

    public String FriendID ;
    public String CatalogName ;

    public byte[] serialize() throws Exception {
        ByteBuf body = BufferUtils.newBuffer();
        byte[] bCatalogName = CatalogName.getBytes(StandardCharsets.UTF_8);
        byte[] bFriendID = FriendID.getBytes(StandardCharsets.UTF_8);
        int bodyLen = 4 + 4 + bCatalogName.length + 4 + bFriendID.length;
        body.writeInt(bodyLen);
        body.writeInt(bCatalogName.length);
        body.writeBytes(bCatalogName);
        body.writeInt(bFriendID.length);
        body.writeBytes(bFriendID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }
}
